<?php

/**
 *  swoole 应用程序入口文件，请勿修改
 */

use Lib\SwooleApp;

function IsTestMechine()
{
    $aryIps = array();
    //$aryIps[] = "10.219.43.211";
    $aryIps[] = "100.96.86.154";

    $aryIps[] = "10.205.142.216"; //x1
    $aryIps[] = "10.217.187.121"; //x7
    $aryIps[] = "10.210.138.189"; //x6
    $aryIps[] = "10.210.157.107"; //x8
    $aryIps[] = "10.239.168.38";  //x5 x2 x9 qqgame
    $aryIps[] = "10.239.168.238";  //tianmei mofang guangzi

    $aryIps[] = "9.68.248.92";  //tianmei mofang guangzi
    $aryIps[] = "100.113.226.202";
    $aryIps[] = "9.67.190.113";
    $aryIps[] = "9.67.190.117";
    $aryIps[] = "100.113.226.215";
    $aryIps[] = "9.66.170.27";
    $aryIps[] = "100.113.228.100";

    $ips = swoole_get_local_ip();

    //print_r($ips);
    for ($i = 0; $i < count($aryIps); $i++) {
        foreach ($ips as $k => $v) {
            if (strpos($aryIps[$i], $v) === false) {

            } else {
                return true;
            }
        }
    }

    return false;
}

//测试机器判断
$g_is_test_mechine = false;
if (IsTestMechine()) {
    //echo "test mechine";
    error_reporting(E_ALL);
    $g_is_test_mechine = true;
} else {
    //echo "normal mechine";
    error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
}

//错误日志和时区
ini_set('display_errors', 'On');
date_default_timezone_set("PRC");

//加载env.php swoole 端口 ip等配置文件
if (file_exists("env.php")) {
    require_once "env.php";
} else {
    //以下内容存在env.php 不在目录中，可以登录机器查看
    //如果是本地开发，可以自定义改，端口不占用即可
    define('AMS_SERVER_PORT', 10271);
    define("AMS_SERVER_HOST", "127.0.0.1");
    define("AMS_SERVER_TNM", "1412688");
    define("PROCESS_NUM", "6");
}

define('APP_PATH', __DIR__);

$strCurlDir = dirname(__FILE__);
$aryCurlDir = explode("/", $strCurlDir);
$strDirName = $aryCurlDir[3];

//日志
define('AMS_SERVER_NAME', $strDirName);
define('AMS_SERVER_LOG', APP_PATH . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . AMS_SERVER_NAME . '.log');
define('LOG_PATH', APP_PATH . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR);

//swoole
define('OSS_SWOOLE_PHP7LIB', "/data/app/ieod-web/php7/oss_swoole");
define('OSS_NOT_SWOOLE', 0);

//TODO 判断运行环境
if (1) {
    define('RUN_ENV', 'internal');
} else {
    ini_set('display_errors', 'On');
    error_reporting(E_ALL ^ E_NOTICE);

    define('RUN_ENV', 'ulink');
    define('CONFIG_PATH', '/data/website/configweb/cfg/ulink/config');
}

//配置文件
require_once APP_PATH . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . RUN_ENV . '.inc.php';

//环境判断
if (defined('ENV') && ENV == 'test') {
    define('NTX_PATH', APP_PATH);
} else {
    define('NTX_PATH', '/data/website/apps7web/htdocs/ntxframework');

    require('/data/app/ieod-web/php7/oss_swoole/OssDefines.inc.php');
    require('/data/app/ieod-web/php7/oss_swoole/swoole_ossllib.inc.php');
}


//加载全局配置文件
define('NTX_LIB', NTX_PATH . DIRECTORY_SEPARATOR . 'Lib');

//启动文件
require NTX_LIB . DIRECTORY_SEPARATOR . 'Loader.php';

//注册
\Loader::register();

//开始启动
$app = new SwooleApp();

//启动参数
if ($argc <= 1) {
    echo 'argc must 2';
    exit(1);
}

if ($argv[1] == 'start') {
    $app->start();
} else if ($argv[1] == 'restart') {
    $app->stop();
    sleep(1);
    $app->start();
} else if ($argv[1] == 'stop') {
    $app->stop();
} else {
    echo 'argv[1] must start or stop';
    exit (1);
}
